package com.example.hibernate.demo.config;

import com.example.hibernate.demo.entity.*;
import com.example.hibernate.demo.repository.*;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@RequiredArgsConstructor
public class DataInitializerPart2 {

    private final DepartmentRepository departmentRepository;
    private final EmployeeRepository employeeRepository;
    private final ProjectRepository projectRepository;
    private final ProductRepository productRepository;
    private final OrderRepository orderRepository;
    private final PaymentRepository paymentRepository;

    @Transactional
    public void initProjectsAndProducts() {
        // 获取员工
        Employee john = employeeRepository.findByEmail("john.doe@example.com").orElse(null);
        Employee jane = employeeRepository.findByEmail("jane.smith@example.com").orElse(null);
        
        if (john == null || jane == null) {
            return;
        }
        
        // 创建项目
        Project crmProject = Project.builder()
                .name("CRM System")
                .description("Customer Relationship Management System")
                .startDate(LocalDate.of(2023, 1, 15))
                .endDate(LocalDate.of(2023, 12, 31))
                .status(Project.ProjectStatus.IN_PROGRESS)
                .build();

        Project erp = Project.builder()
                .name("ERP Implementation")
                .description("Enterprise Resource Planning System")
                .startDate(LocalDate.of(2023, 3, 1))
                .endDate(LocalDate.of(2024, 2, 28))
                .status(Project.ProjectStatus.PLANNING)
                .build();

        projectRepository.saveAll(Arrays.asList(crmProject, erp));

        // 分配员工到项目
        john.addProject(crmProject);
        jane.addProject(crmProject);
        jane.addProject(erp);
        employeeRepository.saveAll(Arrays.asList(john, jane));

        // 创建产品
        Product laptop = Product.builder()
                .name("Business Laptop")
                .description("High-performance laptop for business use")
                .price(new BigDecimal("1299.99"))
                .stockQuantity(50)
                .category(Product.ProductCategory.ELECTRONICS)
                .build();

        Product monitor = Product.builder()
                .name("27-inch Monitor")
                .description("Ultra HD monitor with adjustable stand")
                .price(new BigDecimal("349.99"))
                .stockQuantity(30)
                .category(Product.ProductCategory.ELECTRONICS)
                .build();

        Product desk = Product.builder()
                .name("Standing Desk")
                .description("Adjustable height standing desk")
                .price(new BigDecimal("599.99"))
                .stockQuantity(15)
                .category(Product.ProductCategory.HOME)
                .build();

        productRepository.saveAll(Arrays.asList(laptop, monitor, desk));
    }

    @Transactional
    public void initOrdersAndPayments() {
        // 获取产品
        Product laptop = productRepository.findAll().stream()
                .filter(p -> p.getName().equals("Business Laptop"))
                .findFirst().orElse(null);
        
        Product monitor = productRepository.findAll().stream()
                .filter(p -> p.getName().equals("27-inch Monitor"))
                .findFirst().orElse(null);
        
        if (laptop == null || monitor == null) {
            return;
        }
        
        // 创建订单
        Order order1 = Order.builder()
                .customerName("Alice Brown")
                .customerEmail("alice.brown@example.com")
                .shippingAddress("789 Pine St, Somewhere")
                .status(Order.OrderStatus.DELIVERED)
                .orderDate(LocalDateTime.now().minusDays(30))
                .totalAmount(new BigDecimal("1649.98"))
                .build();
        
        orderRepository.save(order1);
        
        // 添加订单项
        OrderItem item1 = new OrderItem();
        item1.setId(new OrderItem.OrderItemId(order1.getId(), laptop.getId()));
        item1.setOrder(order1);
        item1.setProduct(laptop);
        item1.setQuantity(1);
        item1.setPrice(laptop.getPrice());
        order1.addItem(item1);
        
        OrderItem item2 = new OrderItem();
        item2.setId(new OrderItem.OrderItemId(order1.getId(), monitor.getId()));
        item2.setOrder(order1);
        item2.setProduct(monitor);
        item2.setQuantity(1);
        item2.setPrice(monitor.getPrice());
        order1.addItem(item2);
        
        orderRepository.save(order1);
        
        // 创建支付
        CreditCardPayment ccPayment = new CreditCardPayment();
        ccPayment.setAmount(new BigDecimal("1649.98"));
        ccPayment.setStatus(Payment.PaymentStatus.COMPLETED);
        ccPayment.setCardNumber("**** **** **** 1234");
        ccPayment.setCardHolderName("Alice Brown");
        ccPayment.setExpirationDate("12/2025");
        
        paymentRepository.save(ccPayment);
        
        BankTransferPayment btPayment = new BankTransferPayment();
        btPayment.setAmount(new BigDecimal("599.99"));
        btPayment.setStatus(Payment.PaymentStatus.PENDING);
        btPayment.setBankName("First National Bank");
        btPayment.setAccountNumber("987654321");
        btPayment.setAccountHolderName("Bob Johnson");
        
        paymentRepository.save(btPayment);
    }
}